package com.cheyunkeji.er.activity;

import android.content.Intent;
import android.os.Handler;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.cheyun.netsalev3.MyApplication;
import com.cheyun.netsalev3.R;
import com.cheyun.netsalev3.bean.login.LoginParam;
import com.cheyun.netsalev3.bean.login.MemberX;
import com.cheyun.netsalev3.utils.MySharedPreferences;
import com.cheyunkeji.er.Constants;
import com.cheyunkeji.er.activity.evaluate.EvaluateHomePageActivity;
import com.cheyunkeji.er.activity.fast_evaluate.FastEvaluateActivity;
import com.cheyunkeji.er.app.GlobalConfig;
import com.cheyunkeji.er.app.UserInfoManager;
import com.cheyunkeji.er.base.BaseActivity;
import com.cheyunkeji.er.bean.UserInfo;
import com.cheyunkeji.er.bean.UserInfoToken;
import com.cheyunkeji.er.bean.evaluate.CarTempAttrList;
import com.cheyunkeji.er.http.ApiClient;
import com.cheyunkeji.er.http.HttpConstants;
import com.cheyunkeji.er.http.RespCallback;
import com.cheyunkeji.er.utils.ChannelInfoUtil;
import com.cheyunkeji.er.utils.SerializeUtil;
import com.cheyunkeji.er.view.SToast;

import java.util.HashMap;

import butterknife.BindView;

public class SplashActivity extends BaseActivity {

  private final int SPLASH_DISPLAY_LENGHT = 500;

  @BindView(R.id.iv_splash)
  ImageView ivSplash;

  @BindView(R.id.activity_splash)
  RelativeLayout activitySplash;

  private Handler handler = new Handler();

  private Integer moduleType = 1;

  @Override
  protected void init() {
    setContentView(R.layout.er_activity_splash);
  }

  @Override
  protected void initView() {
    if (getIntent().hasExtra("module_type")) {
      moduleType = getIntent().getIntExtra("module_type", 1);
    }

    handler.postDelayed(r, SPLASH_DISPLAY_LENGHT);
  }

  Runnable r =
          new Runnable() {
            @Override
            public void run() {
              login();
            }
          };

  @Override
  protected void initData() {}


  private void login() {
      goChannel();
      cacheCarTempInfo();
  }

  private void saveUserInfo(UserInfoToken data) {
    MemberX userData = MySharedPreferences.INSTANCE.getLoginData().getMember();
    if (null != data) {
      //            sp存储用户关键信息
      UserInfoManager.getInstance(MyApplication.getInstance())
          .saveMid(data.getMid())
          .saveMobile(userData.getMobile())
          .saveGender(userData.getSex() + "")
          .saveRealName(userData.getRealname())
          .saveAuthKey(data.getAuthkey());
      GlobalConfig.getInstance(getApplicationContext()).setLogined(true);
    }
  }


    private void goChannel() {
        LoginParam loginParam = MySharedPreferences.INSTANCE.getLoginData();
        UserInfo.ChannelListBean channelListBean = new UserInfo.ChannelListBean();
        channelListBean.setUid(loginParam.getMember().getUid() + "");
        channelListBean.setChannel(loginParam.getMember().getChannel() + "");
        channelListBean.setIsadmin(0);
        channelListBean.setIsarchiver(1);
        channelListBean.setIsrapid(1);
        channelListBean.setIsauction(1);
        channelListBean.setY7e_ckey("");

        ChannelInfoUtil.saveCurrentChannelInfo(channelListBean);

        MemberX userData = loginParam.getMember();
        UserInfoManager.getInstance(MyApplication.getInstance())
                .saveMobile(userData.getMobile())
                .saveGender(userData.getSex() + "")
                .saveRealName(userData.getRealname());
        GlobalConfig.getInstance(getApplicationContext()).setLogined(true);

        UserInfoManager.getInstance(MyApplication.getInstance())
                .saveY7e_ckey(channelListBean.getY7e_ckey());

        if (moduleType == Constants.CURRENT_MODULE_TYPE_FAST_EVALUATE) {
            MyApplication.getInstance().setCurrentModuleType(Constants.CURRENT_MODULE_TYPE_FAST_EVALUATE);
            startActivity(new Intent(this, FastEvaluateActivity.class));
        } else {
            MyApplication.getInstance().setCurrentModuleType(Constants.CURRENT_MODULE_TYPE_FULL_EVALUATE);
            startActivity(new Intent(this, EvaluateHomePageActivity.class));
        }

        finish();
    }

  private void cacheCarTempInfo() {
    HashMap<String, String> params = new HashMap<>();
    params.put("field", HttpConstants.TEMP_ATTR_LIST_REQUEST_FILED);
    ApiClient.postForm(
        HttpConstants.CAR_TEMP_ATTR_LIST,
        params,
        new RespCallback<CarTempAttrList>() {

          @Override
          protected void onRequestFailed(String msg) {
            SToast.show(msg);
            //                llChannelCompany.setVisibility(View.INVISIBLE);
            //                btnJoin.setOnClickListener(ChannelSelectActivity.this);
            //                tvCreateOrganization.setOnClickListener(ChannelSelectActivity.this);
            //                tvCreateOrganization.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
            //                tvCreateOrganization.getPaint().setAntiAlias(true);
          }

          @Override
          protected void onSuccess(CarTempAttrList data) {
            if (null != data) {
              SerializeUtil.saveCarTempAttrList(data);
              MyApplication.getInstance().setAttrList(data);
            }
          }
        });
  }
}
